package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.eotms.dto.ServiceProductDto;
import cn.buk.tms.eotms.dto.VasOrderDto;
import cn.buk.tms.eotms.entity.VasOrder;
import cn.buk.tms.eotms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.eotms.service.ProductService;
import cn.buk.tms.eotms.service.VasOrderService;
import cn.buk.tms.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * value-added airport service 增值机场服务
 * TODO 临时从tms代码中复制过来。今后将把从捷西接口对接的部分独立出来。 2020-03-25 yfdai
 * 机场服务，调用接口
 * @author yfdai
 */
@RestController
@RequestMapping("/vaas")
public class VaasController extends BaseController {

  @Autowired
  private ProductService productService;

  @Autowired
  private VasOrderService vasOrderService;


  /**
   * 创建增值服务订单
   */
  @RequestMapping(value = "/order", consumes = "application/json", method = RequestMethod.POST)
  public JsonResult createVaasOrder(HttpServletRequest request,
                                   @RequestBody VasOrderDto dto) throws BaseException {
    final int userId = getUserId(request);
    final String operator = getUsername(request);

    return vasOrderService.createVaasOrder(userId, operator, dto);
  }

  /**
   * 获取vaas订单详情
   */
  @GetMapping("/order/{id}")
  public VasOrderDto getVasOrderDetail(HttpServletRequest request,
                                       @PathVariable("id") int id
  ) {
    return vasOrderService.getVasOrderById(getUserId(request), id);
  }

  @GetMapping("/orders")
  public CommonDto<VasOrderDto> searchVaasOrders(HttpServletRequest request,
                                                @ModelAttribute("sc") SearchFlightOrderCriteria sc
  ) {
    sc.setOrderType(VasOrder.VAS_ORDER_TYPE_VAAS);
    sc.setCustomerId(-1);

    List<VasOrderDto> datas = vasOrderService.searchVasOrders(getUserId(request), sc, false);

    CommonDto<VasOrderDto> commonDto = new CommonDto<>();
    commonDto.setDataList(datas);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 为机场服务订单付款
   */
  @PostMapping(value = "/order/{id}/pay")
  public JsonResult payVaasOrder(HttpServletRequest request,
                                      @PathVariable("id") int id
  ) {
    return vasOrderService.payVaasOrder(getUserId(request), id);
  }
}
